package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.example.entity.Department;
import org.example.entity.Post;
import org.example.models.R;
import org.example.service.DepartmentService;
import org.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private PostService postService;

    // 查询所有部门
    @GetMapping("/find")
    public R<List<Department>> findAll() {
        List<Department> list = departmentService.list();
        if (list == null) {
            return R.error("数据出错");
        }
        return R.success(list);
    }

    // 根据post_code查询所有内容
    @GetMapping("/findDeptAndPost")
    public R<List<Post>> findDeptAndPost(@Param("postCode") String postCode) {
        List<Post> list = postService.queryEverythingAccordingToPost_code(postCode);
        return R.success(list);
    }

    // 根据dept_id查询所有内容
    @GetMapping("/findDeptAndPostById")
    public R<List<Department>> findDeptAndPostById(@Param("deptId") String deptId) {
        deptId = "200002";
        List<Department> list = departmentService.queryTheContentBasedOnTheId(deptId);
        return R.success(list);
    }

    @GetMapping("/finds")
    public R<List<Department>> finds(
            @RequestParam(name = "deptId", required = false) Integer deptId // 将参数类型改为Integer
    ) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (deptId != null) {
            queryWrapper.lambda().eq(Department::getDeptId, deptId);
        }
        List<Department> empList = departmentService.list(queryWrapper);
        return R.success(empList);
    }



    // 为岗位调动模块添加的接口
    @GetMapping("/list")
    public R<List<Department>> list() {
        try {
            // 只获取启用的部门
            List<Department> departmentList = departmentService.list();
            return R.success(departmentList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取部门列表失败: " + e.getMessage());
        }
    }

//    /*获取部门与其组织架构详情*/
    @GetMapping("/listWithStructure")
    public R<List<Department>> listWithStructure(@RequestParam(name = "deptId", required = false) Integer deptId) {
        try {
            List<Department> departmentList = departmentService.selectDepartmentWithStructure(deptId);
            return R.success(departmentList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("获取部门列表失败: " + e.getMessage());
        }
    }
}

